package google.android.ageofempires;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Bitmap.Config;

public class Utility {
	
	public void rotateBitmap(Bitmap bitmap, float degrees) {
		
	}
	
	public static double getDegree(Point p1, Point p2) {		
		int xDistance = p2.x - p1.x;
		int yDistance = p2.y - p1.y;
		double distance = Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
		double sinAlpha = (double)(yDistance / distance);
		double alpha = Math.toDegrees(Math.asin(sinAlpha));
		if(alpha > 0 && xDistance > 0) {
			alpha = alpha;
		}
		else if(alpha > 0 && xDistance < 0) {
			alpha = 180 - alpha;
		}
		else if(alpha < 0 && xDistance > 0) {
			alpha += 360;
		}
		else if(alpha < 0 && xDistance < 0) {
			alpha += 270;
		}
		return alpha;
	}
	
	public static Bitmap Transparency(Bitmap seed)
	{		
		int mWidth = seed.getWidth();
		int mHeight = seed.getHeight();
		int[] mArrayColor = new int[mWidth * mHeight];			
		Bitmap mFullTree = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
		Canvas mFullTreeCanvas = new Canvas(mFullTree);
		
		try
		{
			// Lay mau cua pixel goc tren ben trai
			int mColor = seed.getPixel(0, 0);		
			// Lay mau cua tat ca pixel cua hinh
			seed.getPixels(mArrayColor, 0, mWidth, 0, 0, mWidth, mHeight);					
			for(int i = 0; i < mArrayColor.length; i++) {
				// So sanh mau: 
				// Neu mau i trong mArrayColor = mColor -> lam mat mau nay
				if(mArrayColor[i] == mColor) {
					mArrayColor[i] = 0;
				}
			}			
			mFullTreeCanvas.drawBitmap(mArrayColor, 0, mWidth, 0, 0, mWidth, mHeight, true, null);//						
		}
		catch(IllegalArgumentException e)
		{
			e.printStackTrace();
		}
		return mFullTree;
	}
}
